package com.example.spinnertest_618lw;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView tvinfo;
    private Spinner spinner;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        tvinfo=findViewById(R.id.textView);
        spinner=findViewById(R.id.spinner2);
        //数据源
        String[] cities={"北京","上海","大连","天津"};
        final ArrayAdapter<String>  adapter=
                new ArrayAdapter<String>(
                        this,
                        android.R.layout.simple_spinner_item,
                        cities
                );
        adapter.setDropDownViewResource(
                android.R.layout.
                        simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);
        spinner.setPrompt("请选择城市:");


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                tvinfo.setText(
                        "您选择的是："
                        +adapterView.getItemAtPosition(i).toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }
}
